package com.ruoyi.service.achievements.impl;

import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.mapper.achievements.AcvEmpManageMapper;
import com.ruoyi.service.achievements.AcvEmpManageService;
import com.ruoyi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author zeo
 * @explain 绩效人员管理服务实现类
 * @date 2020/8/24 9:45
 */
@Service
public class AcvEmpManageServiceImpl implements AcvEmpManageService {

    @Autowired
    private AcvEmpManageMapper acvEmpManageMapper;

    @Autowired
    private SysPasswordService passwordService;

    @Override
    public int addExamine(Map map) {
        String loginName = map.get("loginName").toString();
        String userName = map.get("userName").toString();
        String deptId = map.get("deptId").toString();
        String tertiaryDeptId = map.get("tertiaryDeptId").toString();
        String salt = ShiroUtils.randomSalt();
        String password = passwordService.encryptPassword(loginName, "123456", salt);
        Map<String, String> map1 = new HashMap<String, String>();
        map1.put("loginName", loginName);
        map1.put("userName", userName);
        map1.put("deptId", deptId);
        map1.put("salt", salt);
        map1.put("password", password);
        map1.put("tertiaryDeptId", tertiaryDeptId);

        int num = acvEmpManageMapper.addExamine(map1);
        //角色
        acvEmpManageMapper.addScoringRole();
        return num;
    }

    @Override
    public int findCountByUserName(String userName) {
        return acvEmpManageMapper.findCountByUserName(userName);
    }

    @Override
    public int findCountByLoginName(String loginName) {
        return acvEmpManageMapper.findCountByLoginName(loginName);
    }

    @Override
    public Map findUserInfo(String userId) {
        return acvEmpManageMapper.findUserInfo(userId);
    }

    @Override
    public List<Map> list(Map map) {
        List<Map> maps = acvEmpManageMapper.list(map);
        if (map.size() > 0) {
            if (!"".equals(map.get("userName").toString()) || !"".equals(map.get("deptId").toString()) || !"".equals(map.get("tertiaryDeptId").toString())) {
                String examinListId = "";
                String examinListName = "";
                for (Map map1 : maps) {
                    examinListId += "'" + map1.get("userId").toString() + "'" + ",";
                    examinListName += "'" + map1.get("userName").toString() + "'" + ",";
                }
                examinListId = examinListId.substring(0, examinListId.length() - 1);
                examinListName = examinListName.substring(0, examinListName.length() - 1);
                List<Map> maps1 = acvEmpManageMapper.findSuperiorAndSubordinate(examinListId, examinListName);


                for (Map map1 : maps1) {
                    String userId = map1.get("userId").toString();
                    boolean isBreak = false;
                    for (Map map2 : maps) {
                        if (userId.equals(map2.get("userId").toString())) {
                            isBreak = true;
                        }
                    }
                    if (isBreak) {
                        continue;
                    }
                    maps.add(map1);
                }
            }
        }
        Collections.sort(maps, new Comparator<Map>() {
            @Override
            public int compare(Map o1, Map o2) {
                if (Integer.parseInt(o1.get("type").toString()) > Integer.parseInt(o2.get("type").toString())) {
                    return 1;
                }
                if (Integer.parseInt(o1.get("type").toString()) == Integer.parseInt(o2.get("type").toString())) {
                    return 0;
                }
                return -1;
            }
        });
        return maps;
    }

    @Override
    public int add(Map map) {
        map.put("examineeId", StringUtil.getUUID());
        return acvEmpManageMapper.add(map);
    }

    @Override
    public int editScoring(Map map) {
        return acvEmpManageMapper.editScoring(map);
    }

    @Override
    public Map findExamineeByUserId(String userId) {
        return acvEmpManageMapper.findExamineeByUserId(userId);
    }

    @Override
    public int edit(Map map) {
        String examineeId = map.get("examineeId").toString();
        Map map1 = acvEmpManageMapper.findExamineeByUserId(examineeId);
        //原来打分人Id
        int originalSuperId = Integer.parseInt(map1.get("examineeSuperId").toString());
        //现在打分人Id
        int nowSuperId = Integer.parseInt(map.get("superId").toString());
        if (originalSuperId == nowSuperId) {
            return acvEmpManageMapper.edit(map);
        } else {
            //将原来改成失效状态
            acvEmpManageMapper.remove(examineeId);
            //新增
            map.put("id", StringUtil.getUUID());
            return acvEmpManageMapper.addExaminee(map);
        }
    }

    @Override
    public int remove(String userId) {
        return acvEmpManageMapper.remove(userId);
    }

    @Override
    public int findExamineeCountBySuperUserId(int superId) {
        return acvEmpManageMapper.findExamineeCountBySuperUserId(superId);
    }

    @Override
    public int findEmpCountBySuperUserId(int superId) {
        return acvEmpManageMapper.findEmpCountBySuperUserId(superId);
    }

    @Override
    public int updateScoreUserInvalid(int userId) {
        return acvEmpManageMapper.updateScoreUserInvalid(userId);
    }

    @Override
    public List<Map<String, Object>> findScorerList(String userId) {
        return acvEmpManageMapper.findScorerList(userId);
    }

    @Override
    public Map findTwoWay(String name, String badge) {
        return acvEmpManageMapper.findTwoWay(name, badge);
    }

    @Override
    public int insertTwoWay(Map map) {
        map.put("id", StringUtil.getUUID());
        return acvEmpManageMapper.insertTwoWay(map);
    }

    @Override
    public int findTwoWayCount(String name, String badge) {
        return acvEmpManageMapper.findTwoWayCount(name, badge);
    }

    @Override
    public int deleteTwoWay(String name, String badge) {
        return acvEmpManageMapper.deleteTwoWay(name, badge);
    }
}
